package com.cjf.aop.annotation_demo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * created by cjf
 * on: 2020/3/19
 */
@ClassAnnotation(age = 25)
public class Test {

    /**
     * Class注解测试
     * @param c
     */
    public static void classTest(Class<?> c) {
        ClassAnnotation ca = c.getAnnotation(ClassAnnotation.class);
        if (ca == null) {
            System.out.println("UnKnown ClassAnnotation");
        } else {
            System.out.println("age:" + ca.age());
        }
    }

    /**
     * Method注解测试
     * @param c
     *
     */
    public static void methodTest(Class<?> c){
        Method[] methods = c.getDeclaredMethods();
        for (Method me:methods){
            MethodAnnotation ma = me.getAnnotation(MethodAnnotation.class);
            if (ma==null){
                System.out.println("UnKnown MethodAnnotation");
            }else {
                System.out.println("name:"+ma.name());
                System.out.println("value:"+ma.value());
            }
        }
    }

    public static void fieldTest(Class<?> c){
        Field[] fields = c.getDeclaredFields();
        for (Field field:fields){
            FieldAnnotation fa = field.getAnnotation(FieldAnnotation.class);
            if (fa==null){
                System.out.println("UnKnown FieldAnnotation");
            }else {
                System.out.println("name:"+fa.name());
                System.out.println("description:"+fa.description());
            }
        }
    }

    public static void main(String[] args){
        Test.classTest(Test.class);
        System.out.println("===================================");
        Test.methodTest(MyName.class);
        System.out.println("===================================");
        Test.fieldTest(MyName.class);
        System.out.println("===================================");
    }
}
